package csc.fresher.internetbanking;

import java.util.List;

import javax.faces.context.FacesContext;

import mockit.*;
import mockit.integration.junit4.JMockit;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import csc.fresher.dao.CustomerDao;
import csc.fresher.dao.DepositAccountDao;
import csc.fresher.dao.impl.*;
import csc.fresher.entity.AccountHasInterestRate;
import csc.fresher.entity.AccountState;
import csc.fresher.entity.Customer;
import csc.fresher.entity.CustomerAccount;
import csc.fresher.entity.DepositAccount;
import csc.fresher.entity.DepositTransaction;
import csc.fresher.entity.InternetAccount;
import csc.fresher.entity.SavingAccount;
import csc.fresher.service.DepositAccountTransactionServiceImpl;
import csc.fresher.service.InternetLoginServiceImpl;
import csc.fresher.view.InternetLoginManageBean;
import csc.fresher.view.TransferMoneyManageBean;
import static org.mockito.Mockito.*;
@RunWith(JMockit.class)
public class ValidateTransaction {
	private TransferMoneyManageBean ser;
	private FacesContext c;
	@Before
	public void init() {
		c = mock(FacesContext.class);
		new MockUp<InternetLoginManageBean>() {
			InternetAccount acc;
			
			@Mock
			public void $init() {
				if (acc == null) {
					acc = new InternetAccount();
					acc.setCustomerID(01234567);
					acc.setLock(false);
					acc.setPassword("123456");
					acc.setUsername("a123");
				}
			}

			@Mock
			public InternetAccount getAcc() {
				return acc;
			}
		};
		new MockUp<DepositAccountTransactionServiceImpl>() {
			@Mock
			public long transferFunds(DepositTransaction transaction) {
			
				return 1;
				
			}
		};
		
		new MockUp<DepositAccountDaoImpl>() {
			DepositAccount accTransfer;
			DepositAccount accActive1;
			DepositAccount accActive2;
			DepositAccount accReceive;

			@Mock
			public void $init() {
				if (accTransfer == null) {
					accTransfer = new DepositAccount();
					accTransfer.setAccountState(AccountState.DISABLE);
					accTransfer.setAccountBalance(100000);
				}
				if (accReceive == null) {
					accReceive = new DepositAccount();
					accReceive.setAccountState(AccountState.DISABLE);
					accReceive.setAccountBalance(100000);
				}
				if (accActive1 == null) {
					accActive1 = new DepositAccount();
					accActive1.setAccountState(AccountState.ACTIVE);
					accActive1.setAccountBalance(100000);
				}
				if (accActive2 == null) {
					accActive2 = new DepositAccount();
					accActive2.setAccountState(AccountState.ACTIVE);
					accActive2.setAccountBalance(100000);
				}
			}

			@Mock
			public DepositAccount getDepositAccountByDepositAccountID(long id) {
				if (id == 1)
					return accTransfer;
				else if(id==2)
					return accReceive;
				else if(id==3)
					return accActive1;
				else if(id==4)
					return accActive2;
				else
					return null;
			}
		};
		new MockUp<CustomerDaoImpl>() {
			Customer cusTransfer;
			Customer receiveTransfer;
			@Mock
			public Customer getCustomerByID(long customerID) {
				return cusTransfer;
			}
		};
	}


	@Test
	public void TestConfirmTransfer() {
		ser = new TransferMoneyManageBean();
		ser.setDepositDao(new  DepositAccountDaoImpl());
		ser.setCmb(new InternetLoginManageBean());
		ser.setCustomerDao(new CustomerDaoImpl());
		ser.setAccounNumChoosen((long) 1);
		ser.setAccountNumber((long) 2);
		ser.setC(this.c);
		ser.setFund((double) 20000);
		Assert.assertEquals("login", "transfer", ser.confirmTransfer());
		ser.setAccounNumChoosen((long) 3);
		Assert.assertEquals("login", "transfer", ser.confirmTransfer());
		ser.setAccountNumber((long) 5);
		Assert.assertEquals("login", "transfer", ser.confirmTransfer());
		ser.setAccountNumber((long) 3);
		Assert.assertEquals("login", "transfer", ser.confirmTransfer());
		ser.setAccountNumber((long) 4);
		Assert.assertEquals("login", "confirmtransfer", ser.confirmTransfer());
		ser.setFund((double) 20000000);
		Assert.assertEquals("login", "transfer", ser.confirmTransfer());
	}
	@Test
	public void TestTransfer() {
		ser = new TransferMoneyManageBean();
		ser.setFund((double) 20000);
		DepositAccount transferAcc = new DepositAccount();
		transferAcc.setAccountBalance(100000);
		DepositAccount receiveAcc = new DepositAccount();
		receiveAcc.setAccountBalance(100000);
		ser.setTransferAcc(transferAcc);
		ser.setReceiveAcc(receiveAcc);
		ser.setDepositAccountTransactionServiceImpl(new DepositAccountTransactionServiceImpl());
		Assert.assertEquals("login", "transfercomplete", ser.transfer());

	}
}
